<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <!--[if IE]>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <![endif]-->
    <title>魅力野兽</title>
    <meta name="description" content="魅力野兽" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="format-detection" content="telephone=no" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
    <link rel="stylesheet" type="text/css" href="css/style.css">
   
</head>
<body>
    <div>
        测试微信接口
    </div>
    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript">
        var TD = TD || {};

        //美林版ajax对应接口
        TD.ajax = function(pm, succback, errorback){
            $.ajax({
                type: pm.type || 'GET',
                url: pm.url,
                dataType: 'json',
                data: pm.data || '',
                success: function(data){
                    if (data.status == 1) {
                        succback && succback(data.data);
                    }else {
                        errorback && errorback(data.message);
                    }
                },
                error: function(xhr, status, thrown){
                    errorback && errorback('网络连接不稳定，请重试或刷新页面！');
                }
            });
        };

        /*data参数说明
        data = {
            title: string, 朋友圈标题，给朋友的描述
            desc: string, 给朋友的标题
            link: string, 链接
            img: string, 图标
            track: string, 分享数据上报地址,post {btn:'1'}朋友或{btn:'2'}朋友圈
        }
        */
        TD.wxShare = function(data, callback){
            wx.onMenuShareTimeline({
                title: data.title, // 分享标题
                link: data.link, // 分享链接
                imgUrl: data.img, // 分享图标
                success: function () { 
                    callback && callback();
                    //上报朋友圈
                    TD.ajax({
                        type: 'POST',
                        url: data.track,
                        data: {
                            btn: '2'
                        }
                    }, function(data){
                        console.log(data);
                    });
                },
                cancel: function () { 
                    // 用户取消分享后执行的回调函数
                }
            });
            wx.onMenuShareAppMessage({
                title: data.desc, // 分享标题
                desc: data.title, // 分享描述
                link: data.link, // 分享链接
                imgUrl: data.img, // 分享图标
                success: function () { 
                    callback && callback();
                    //上报朋友
                    TD.ajax({
                        type: 'POST',
                        url: data.track,
                        data: {
                            btn: '1'
                        }
                    }, function(data){
                        console.log(data);
                    });
                },
                cancel: function () { 
                    // 用户取消分享后执行的回调函数
                }
            });
        };
        
        //初始化微信接口
        //注意，与微信标准data相比，这里多了data.share属性，对应的是初始化页面时有默认的分享数据
        TD.initWxApi = function(data, succback){
            wx.config({
                debug: true, 
                appId: data.appId, 
                timestamp: data.timestamp, 
                nonceStr: data.nonceStr, 
                signature: data.signature,
                jsApiList: [
                    'onMenuShareTimeline', 
                    'onMenuShareAppMessage', 
                    'startRecord', 
                    'stopRecord', 
                    'onVoiceRecordEnd', 
                    'playVoice', 
                    'pauseVoice', 
                    'stopVoice', 
                    'onVoicePlayEnd', 
                    'uploadVoice', 
                    'downloadVoice',
                    'chooseImage',
                    'previewImage',
                    'uploadImage',
                    'downloadImage'
                ] 
            });
            wx.ready(function(){
                TD.wxShare(data.share);
            });
        };
        
    </script>
    <script type="text/javascript" src="http://meili.treedom.cn/js/lib/zepto.min.js"></script>
    <script type="text/javascript">
    (function(){
        var Config = Config || {};
    
        //初始化JS SDK
        Config.URL_initSDK = 'http://meili.treedom.cn/back/index.php?m=sub&c=wechat&a=signature';

        //默认分享语
        Config.defShare = {
            title: '不看脸，你还敢爱吗？',
            desc: '一头理想主义野兽的恋爱观',
            link: location.href,
            img: 'http://meili.treedom.cn/img/share.jpg',
            track: Config.URL_trackShare
        };

        //初始化微信接口
        TD.ajax({
            url: Config.URL_initSDK,
            type: 'POST',
            data: {
                url: encodeURI(location.href)
            }
        }, function(data){
            //加默认分享语
            data.share = Config.defShare;
            TD.initWxApi(data);
        }, function(msg){
            //初始化失败
            console.log(msg);
        });
    })();
    </script>
</body>
</html>